<?php

class Calcrow extends DirModel
{
	
	public $groupName='group.ut_group_name';
	
	public $viewColumns=array(
		'ut_calcrow_name',
		'ut_calcrow_order',
		'ut_calcrow_formula',
	);
	
	public function getUpdateFields()
	{  
		return array(
			array(
				'name'=>'ut_group_id',
				'editor'=>'dropDown',
				'data'=>CActiveRecord::model('Group')->listData(),
			),
			'ut_calcrow_name',
			'ut_calcrow_order',
			array(
				'name'=>'ut_calcrow_formula',
				'editor'=>'memo',
				'rows'=>6, 
				'cols'=>50,
			),			
		);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ut_calcrow';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ut_calcrow_name, ut_calcrow_order', 'required'),
			array('ut_calcrow_name', 'length', 'max'=>255),
			array('ut_calcrow_formula', 'safe'),
			array('ut_group_id, ut_calcrow_order', 'type', 'type'=>'integer'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'group'=>array(self::BELONGS_TO,'Group','ut_group_id'),
		);
	}

	public function scopes()
	{
		return array(
			'grade'=>array(
				'condition'=>'ut_group_id=2',
			),
			'resource'=>array(
				'condition'=>'ut_group_id=1',
			),
			'select'=>array(
				'condition'=>'ut_calcrow_id=:calcrow_id',
			),
			'absence'=>array(
				'condition'=>'NOT EXISTS (select * from ut_press_calcrow upc WHERE t.ut_calcrow_id=upc.ut_calcrow_id AND ut_press_id=:press)',
			),
		);
	}		
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ut_calcrow_id' => 'Id',
			'ut_calcrow_name' => 'Строка',
			'ut_calcrow_order' => 'Порядок расчета',
			'ut_calcrow_formula' => 'Формула',
		);
	}

	public function search()
	{
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->order='ut_group_id';
      //  $criteria->compare('ut_base_format_id',$this->ut_base_format_id);
      //  $criteria->compare('ut_format_id',$this->ut_format_id);
      //  $criteria->compare('ut_exp_edition',$this->ut_exp_edition);
      //  $criteria->compare('ut_exp_count',$this->ut_exp_count);

        return new CActiveDataProvider(get_class($this), array(
            'criteria'=>$criteria,
        		'pagination'=>false,
        ));
   }
	
}